import PresistNode from "../Utilities/PresistNode";
import MapAssetConfig from "./MapAssetConfig";
import MonsterAssetConfig from "./MonsterAssetConfig";
import EntityTypes from "../Constants/EntityTypes";
import IAsset from "../Interfaces/IAsset";
import MonsterAssetStruct from "../Structs/MonsterAssetStruct";
import MonsterBehaviour from "../Constants/MonsterBehaviour";
import ChapterConfig from "./ChapterConfig";
import MapDatasConfig from "./MapDatasConfig";
import BackgroudConfig from "./BackgroundConfig";
/**
 * 配置文件读取器
 * created by Five on 2018-11-5 12:09:42
 */

const { ccclass } = cc._decorator;

@ccclass
class Config extends cc.Component {
    private static instance: Config = null;

    public static Ins() {
        if (this.instance == null) {
            this.instance = PresistNode("config").addComponent(Config);
            this.instance.init();
        }
        return this.instance;
    }

    public Map: MapDatasConfig = null;
    public MapAsset: MapAssetConfig = null;
    public Monster: MonsterAssetConfig = null;
    public Chapters: ChapterConfig = null;
    public Background: BackgroudConfig = null;

    /**加载必要的文件 */
    private init(): void {
        this.Map = new MapDatasConfig();
        this.MapAsset = new MapAssetConfig();
        this.Monster = new MonsterAssetConfig();
        this.Chapters = new ChapterConfig();
        this.Background = new BackgroudConfig();
    }

    // 判断是否包含某个属性
    public IsTypeOf(mas: IAsset, type: EntityTypes): boolean {
        return (mas.type & type) > 0;
    }

    // 判断是否是可破坏的
    public IsDestoryable(mas: IAsset): boolean {
        return (mas.type & EntityTypes.DESTORYABLE) > 0;
    }

    public IsMonster(asset: IAsset): boolean {
        return Math.round(asset.id / 10000) == 1;
    }

    public IsMonsterBorn(asset: IAsset): boolean {
        return this.IsTypeOf(asset, EntityTypes.MONSTERDOOR);
    }

    public BehaviorLike(asset: MonsterAssetStruct, behaviour: MonsterBehaviour): boolean {
        return (asset.behavior & behaviour) > 0;
    }
}

export default function C() {
    return Config.Ins();
}